package com.supermap.dialogs;

import com.supermap.consts.AttrConsts;
import com.supermap.data.FieldInfo;
import com.supermap.data.FieldInfos;
import com.supermap.dialogs.advance.CJDialog;

import javax.swing.*;
import java.awt.*;

public class OverlayFieldsSettingDialog extends CJDialog {
    private JPanel srcContextPanel;
    private JPanel overlayContextPanel;

    private FieldInfos srcFieldInfos;
    private FieldInfos overlayFieldInfos;
    private String[] selectedSrcFields;
    private String[] selectedOverlayFields;

    public OverlayFieldsSettingDialog(Frame owner, String title, boolean modal, FieldInfos srcFieldInfos, FieldInfos overlayFieldInfos) {
        super(owner, title, modal, 600, 400, new Object[]{srcFieldInfos, overlayFieldInfos});
    }

    @Override
    protected void initializeArgs(Object[] args) {
        this.srcFieldInfos = (FieldInfos) args[0];
        this.overlayFieldInfos = (FieldInfos) args[1];
    }

    @Override
    protected void initializeCore() {
        JPanel centerPanel = new JPanel();
        centerPanel.setLayout(new GridLayout(1, 2));

        JPanel srcPanel = new JPanel();
        srcPanel.setBorder(BorderFactory.createTitledBorder("源数据字段"));
        srcPanel.setLayout(new BorderLayout());

        srcContextPanel = new JPanel();
        srcContextPanel.setLayout(new GridLayout(srcFieldInfos.getCount(), 1));

        for (int i = 0; i < srcFieldInfos.getCount(); i++) {
            FieldInfo fieldInfo = srcFieldInfos.get(i);
            JCheckBox fieldInfoCbx = new JCheckBox();
            fieldInfoCbx.setText(fieldInfo.getName());
            srcContextPanel.add(fieldInfoCbx);
        }

        JScrollPane srcScrollPane = new JScrollPane(srcContextPanel);
        srcScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        srcScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        srcPanel.add(srcScrollPane, BorderLayout.CENTER);
        centerPanel.add(srcPanel);

        JPanel overlayPanel = new JPanel();
        overlayPanel.setBorder(BorderFactory.createTitledBorder("叠加数据字段"));
        overlayPanel.setLayout(new BorderLayout());

        overlayContextPanel = new JPanel();
        overlayContextPanel.setLayout(new GridLayout(overlayFieldInfos.getCount(), 1));

        for (int i = 0; i < overlayFieldInfos.getCount(); i++) {
            FieldInfo fieldInfo = overlayFieldInfos.get(i);
            JCheckBox fieldInfoCbx = new JCheckBox();
            fieldInfoCbx.setText(fieldInfo.getName());
            overlayContextPanel.add(fieldInfoCbx);
        }

        JScrollPane overlayScrollPane = new JScrollPane(overlayContextPanel);
        overlayScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
        overlayScrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        overlayPanel.add(overlayScrollPane, BorderLayout.CENTER);
        centerPanel.add(overlayPanel);

        add(centerPanel, BorderLayout.CENTER);
    }

    @Override
    protected void doConfirmCore() {
        setVisible(false);
        StringBuilder resultStringBuilder = new StringBuilder();
        Component[] srcFieldInfoComponents = srcContextPanel.getComponents();
        for (Component fieldInfoComponent : srcFieldInfoComponents) {
            JCheckBox fieldInfoCbx = (JCheckBox) fieldInfoComponent;
            if (fieldInfoCbx.isSelected()) {
                resultStringBuilder.append(fieldInfoCbx.getText());
                resultStringBuilder.append(AttrConsts.COMMA);
            }
        }
        if (resultStringBuilder.length() > 0) {
            selectedSrcFields = resultStringBuilder.substring(0, resultStringBuilder.length() - 1).split(AttrConsts.COMMA);
            resultStringBuilder = new StringBuilder();
        }
        Component[] overlayFieldInfoComponents = overlayContextPanel.getComponents();
        for (Component fieldInfoComponent : overlayFieldInfoComponents) {
            JCheckBox fieldInfoCbx = (JCheckBox) fieldInfoComponent;
            if (fieldInfoCbx.isSelected()) {
                resultStringBuilder.append(fieldInfoCbx.getText());
                resultStringBuilder.append(AttrConsts.COMMA);
            }
        }
        if (resultStringBuilder.length() > 0) {
            selectedOverlayFields = resultStringBuilder.substring(0, resultStringBuilder.length() - 1).split(AttrConsts.COMMA);
        }
    }

    public String[] getSelectedSrcFields() {
        return selectedSrcFields;
    }

    public String[] getSelectedOverlayFields() {
        return selectedOverlayFields;
    }
}
